package com.sls.game.parser.ohfei;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sls.core.parser.Parser;
import com.sls.game.pojo.OhFeiServerInfo;

/**
 * 
 * @description 殴飞接口，服务端地址xml配置文件解析
 * @author Wangxuegang
 * @date 2012-9-27
 * 
 */
public class OhFeiServerSaxParseXml extends DefaultHandler {

	private List<OhFeiServerInfo> serverList = null;

	private OhFeiServerInfo serverInfo;

	// 用来存放每次遍历后的元素名称(节点名称)
	private String tagName;

	private static Logger log = Logger.getLogger(OhFeiServerSaxParseXml.class);

	/**
	 * 对返回的xml格式字符串数据进行解析
	 * 
	 * @param resultStr
	 * @throws IOException
	 * @throws SAXException
	 * @throws ParserConfigurationException
	 * @throws DocumentException
	 */
	public static List<OhFeiServerInfo> parseXmlString(String filePath) {
		InputStream xmlStream = Parser.class.getClassLoader()
				.getResourceAsStream(filePath);
		try {
			// 构建SAXParser
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();

			OhFeiServerSaxParseXml handler = new OhFeiServerSaxParseXml();
			parser.parse(xmlStream, handler);
			return handler.getServerList();

		} catch (ParserConfigurationException ep) {
			log.error(ep);
		} catch (SAXException es) {
			log.error(es);
		} catch (IOException e) {
			log.error(e);
		}

		return null;
	}

	/**
	 * 读取文件字节流
	 * 
	 * @param filePath
	 * @return
	 */
	@SuppressWarnings("unused")
	private static InputStream readXmlFile(String filePath) {
		FileInputStream fileIn = null;
		try {
			File file = new File(filePath);
			if (file.isFile() && file.exists()) {
				fileIn = new FileInputStream(file);
				return fileIn;
			}
		} catch (Exception e) {
			log.error(e);
		}
		return fileIn;
	}

	// 只调用一次 初始化list集合
	@Override
	public void startDocument() throws SAXException {
		serverList = new ArrayList<OhFeiServerInfo>();
	}

	// 调用多次 开始解析
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (qName.equals("ohFeiServer")) {
			serverInfo = new OhFeiServerInfo();
		}
		this.tagName = qName;
	}

	// 调用多次
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (qName.equals("ohFeiServer")) {
			this.serverList.add(this.serverInfo);
		}
		this.tagName = null;
	}

	// 只调用一次
	@Override
	public void endDocument() throws SAXException {
	}

	// 调用多次
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (this.tagName != null) {
			String data = new String(ch, start, length);
			if (this.tagName.equals("key")) {
				this.serverInfo.setKey(data);
			} else if (this.tagName.equals("address")) {
				this.serverInfo.setAddress(data);
			} else if (this.tagName.equals("userid")) {
				this.serverInfo.setUserid(data);
			} else if (this.tagName.equals("pwd")) {
				this.serverInfo.setUserpws(data);
			} else if (this.tagName.equals("version")) {
				this.serverInfo.setVersion(data);
			} else if (this.tagName.equals("notifyUrl")) {
				this.serverInfo.setNotifyUrl(data);
			} else if (this.tagName.equals("statusQueryAddress")) {
				this.serverInfo.setStatusQueryAddress(data);
			}
		}
	}

	public List<OhFeiServerInfo> getServerList() {
		return serverList;
	}

	public void setServerList(List<OhFeiServerInfo> serverList) {
		this.serverList = serverList;
	}

	public OhFeiServerInfo getServerInfo() {
		return serverInfo;
	}

	public void setServerInfo(OhFeiServerInfo serverInfo) {
		this.serverInfo = serverInfo;
	}

	public String getTagName() {
		return tagName;
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}

}
